package com.dreamstations.changshads.util;

import com.dreamstations.changshads.entity.Article;
import com.dreamstations.changshads.entity.BookDetailBean;
import com.dreamstations.changshads.entity.DatumBean;
import com.dreamstations.changshads.entity.DatumLiteratureBean;
import com.dreamstations.changshads.entity.FigureBean;
import com.dreamstations.changshads.entity.InvestigateBean;
import com.dreamstations.changshads.entity.LibraryBean;
import com.dreamstations.changshads.entity.ResourceBean;
import com.dreamstations.changshads.entity.StoryBean;
import com.dreamstations.changshads.entity.TopicBean;
import com.dreamstations.changshads.entity.WorkBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class DSParser {
    private final static String TAG = DSParser.class.getName();
    private final static Gson gson = new Gson();

    public static Object parse(@RequestType String type, String json) throws Exception {
        L.i(TAG, "=>parse:" + type + "," + json);
        switch (type) {
            case RequestType.TYPE_WORK: {
                return parseWorkBeans(json);
            }
            case RequestType.TYPE_DATUM: {
                return parseDatumBeans(json);
            }
            case RequestType.TYPE_DATUM_LITERATURE: {
                return parseDatumLiteratureBeans(json);
            }
            case RequestType.TYPE_FIGURE: {
                return parseFigureBeans(json);
            }
            case RequestType.TYPE_INVESTIGATE: {
                return parseInvestigateBeans(json);
            }
            case RequestType.TYPE_LIBRARY: {
                return parseLibraryBeans(json);
            }
            case RequestType.TYPE_RESOURCE: {
                return parseResourceBeans(json);
            }
            case RequestType.TYPE_STORY: {
                return parseStoryBeans(json);
            }
            case RequestType.TYPE_TOPIC: {
                return parseTopicBeans(json);
            }
            case RequestType.TYPE_BOOKDETAIL: {
                return parseBookDetail(json);
            }
            case RequestType.PARSE_ARTICLE:{
                return gson.fromJson(json,new TypeToken<List<Article>>() {}.getType());
            }

            default:
                throw new RuntimeException("解析器没有找到对应的解析方法");
        }
    }

    private static List<DatumLiteratureBean> parseDatumLiteratureBeans(String json) {
        return gson.fromJson(json, new TypeToken<List<DatumLiteratureBean>>() {
        }.getType());
    }

    private static List<BookDetailBean> parseBookDetail(String json) {
        return gson.fromJson(json, new TypeToken<List<BookDetailBean>>() {
        }.getType());
    }


    private static List<WorkBean> parseWorkBeans(String json) throws Exception {
//        return gson.fromJson(json, new TypeToken<List<WorkBean>>() {
//        }.getType());
        List<WorkBean> workBeanList=new ArrayList<>();
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover=null;
            try {
                cover = String.valueOf(jsonObject.get("cover"));
            }catch (Exception e){
                L.w(TAG,e.getMessage());
            }
            String time = jsonObject.getString("time");
//            int typeId = jsonObject.getInt("typeId");
//            int proUserId = jsonObject.getInt("proUserId");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            int front=jsonObject.getInt("front");
            workBeanList.add(new WorkBean(id, title, content, cover, time, viewed, template, 0, front));
        }
        return workBeanList;
    }

    private static List<DatumBean> parseDatumBeans(String json) throws Exception {
        return gson.fromJson(json, new TypeToken<List<DatumBean>>() {
        }.getType());
    }

    private static List<FigureBean> parseFigureBeans(String json) throws Exception {
//        return gson.fromJson(json, new TypeToken<List<FigureBean>>() {
//        }.getUrl());
        List<FigureBean> figureBeanList=new ArrayList<>();
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover = jsonObject.getString("cover");
            String time = jsonObject.getString("time");
            String introduce = jsonObject.getString("introduce");
//            int typeId = jsonObject.getInt("typeId");
//            int proUserId = jsonObject.getInt("proUserId");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            boolean front=jsonObject.getBoolean("front");
            figureBeanList.add(new FigureBean(id,title,content,cover,time,viewed,template,0,front,introduce));
        }
        return figureBeanList;
    }

    private static List<InvestigateBean> parseInvestigateBeans(String json) throws Exception {
        return gson.fromJson(json, new TypeToken<List<InvestigateBean>>() {
        }.getType());
    }

    private static List<LibraryBean> parseLibraryBeans(String json) throws Exception {
        /*return gson.fromJson(json, new TypeToken<List<LibraryBean>>() {
        }.getType());*/
        List<LibraryBean> libraryBeanList=new ArrayList<>();
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover = jsonObject.getString("cover");
            String time = jsonObject.getString("time");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            boolean front=jsonObject.getBoolean("front");
            libraryBeanList.add(new LibraryBean(id, title, content, cover, time, viewed, template, 0, front));
        }
        return libraryBeanList;
    }

    private static List<ResourceBean> parseResourceBeans(String json) throws Exception {
        /*return gson.fromJson(json, new TypeToken<List<ResourceBean>>() {
        }.getType());*/
        List<ResourceBean> resourceBeanList=new ArrayList<>();
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover = jsonObject.getString("cover");
            String time = jsonObject.getString("time");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            boolean front=jsonObject.getBoolean("front");
            resourceBeanList.add(new ResourceBean(id, title, content, cover, time, viewed, template, 0, front));
        }
        return resourceBeanList;
    }

    private static List<StoryBean> parseStoryBeans(String json) throws Exception {
        List<StoryBean> storyBeanList=new ArrayList<>();
       /* return gson.fromJson(json, new TypeToken<List<StoryBean>>() {
        }.getType());*/
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover = jsonObject.getString("cover");
            String time = jsonObject.getString("time");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            boolean front=jsonObject.getBoolean("front");
            storyBeanList.add(new StoryBean(id, title, content, cover, time, viewed, template, 0, front));
        }
        return storyBeanList;

    }

    private static List<TopicBean> parseTopicBeans(String json) throws Exception {
//        return gson.fromJson(json, new TypeToken<List<TopicBean>>() {
//        }.getType());
        List<TopicBean> topicBeanList=new ArrayList<>();
        JSONArray jsonArray=new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            long id = jsonObject.getLong("id");
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            String cover = jsonObject.getString("cover");
            String time = jsonObject.getString("time");
//            int typeId = jsonObject.getInt("typeId");
//            int proUserId = jsonObject.getInt("proUserId");
            int viewed = jsonObject.getInt("viewed");
            int template = jsonObject.getInt("template");
            boolean front=jsonObject.getBoolean("front");
            topicBeanList.add(new TopicBean(id, title, content, cover, time, viewed, template, 0, front));
        }
        return topicBeanList;
    }

}
